#coding=utf-8
import os
import urllib
import urllib2
import time
import multiprocessing
from pyquery import PyQuery as pq

#获取图片网址列表
def getUserinfo(pageUrl):
 req=urllib2.Request(pageUrl)
 response=urllib2.urlopen(req).read()
 doc=pq(response)
 fans_count=doc(".ex-userinfo div.span:eq(1) span").text()
 username = doc(".ex-userinfo div.ex-username span:eq(0)").text()
 person_cate=doc(".ex-userinfo div.ex-username span:eq(1)").text()
 cup=doc(".ex-userinfo div.span:eq(0) span").text()
 print "用户名",username+cup
 if fans_count>500:
  return username,cup
 else:
     return ""
#获取img的url
def getImgUrls(pageUrl,name):
 req=urllib2.Request(pageUrl)
 response=urllib2.urlopen(req).read()
 doc=pq(response)
 username = doc(".ex-userinfo div.ex-username span:eq(0)").text()
 imgUrls=[]
 host="https://wxpub.o2bra.com.cn"
 if username==name:
  print username
  imgs=doc('.details-info img')
  for img in imgs:
    imgUrls.append(host+doc(img).attr.src)
 return imgUrls

#保存图片到本地
def saveImg(imgs,dir,num):
    for imgUrl in imgs :
        img=urllib.urlopen(imgUrl).read()
        img_path=dir+str(num)+"_"+str(time.time())+".jpg"
        f = file(img_path,"wb")
        f.write(img)
    f.close()


#开始下载图片
def get_imgs(index,dir,start,end,name):
    for i in range(start,end):
        try:
            url=index+str(i)
            imgUrls = getImgUrls(url,name)
            saveImg(imgUrls,dir,i)
            print url
        except:
            continue
#程序入口
def start(url,dir,count,start,end,name):
    process_list=[]
    index=(end-start)/count
    for i in range(count):
        process_list.append(multiprocessing.Process(target = get_imgs, args = (url,dir,start+index*i,start+index*(i+1),name)))
    process_list.append(multiprocessing.Process(target = get_imgs, args = (url,dir,start+index*i,end+1,name)))
    for process in process_list:
        process.start()
def make_dir(path):
    if  not os.path.exists(path):
        #shutil.rmtree(path)
        os.mkdir(path)

def get_all_imgs(fromIndex,toIndex):
    url = "https://wxpub.o2bra.com.cn/blogs/mobile/"
    begin =0
    end = 5000
    for i in range(fromIndex,toIndex):
        tuple = getUserinfo(url+str(i))
        name = tuple[0]
        cup = tuple[1]
        if name!="":
         dir="D://ox/"+name+cup+"/"
         if(os.path.exists(dir)):
            continue
         else:
            make_dir(dir)
            start(url,dir,5,begin,end,name)

def getImgsByIndex(index):
    url = "https://wxpub.o2bra.com.cn/blogs/mobile/"
    tuple = getUserinfo(url+str(index))
    name = tuple[0]
    cup = tuple[1]
    begin =0
    end = 5000
    dir="D://ox/"+name+cup+"/"
    if(os.path.exists(dir)==False):
      make_dir(dir)
      start(url,dir,20,begin,end,name)
if __name__ == "__main__":
    #getImgsByIndex(1000)
    getImgsByIndex(2752)
